﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Bierdopje.API
{
    public class TextHelper
    {
        private const string cFirstLetter = @" (?<FirstLetter>[^\d\s])|(?<FirstLetter>^\w)";
        private static Regex cRegExCap = new Regex(cFirstLetter, RegexOptions.Compiled);

        public static bool WildcardTextCompare(string searchString, string textToSearch, bool caseSensitive)
        {
            string searchStr = CreateRegexString(searchString); 
            Regex m_Expression;
            if (caseSensitive)
            {
                m_Expression = new Regex(searchStr, RegexOptions.Compiled);
                return m_Expression.Match(textToSearch).Success;
            }
            else
            {
                m_Expression = new Regex(searchStr.ToLower(), RegexOptions.Compiled);
                return m_Expression.Match(textToSearch.ToLower()).Success;
            }
        }

        private static string CreateRegexString(string searchString)
        {
            string[] regexChars = new string[] { @".", @"[", @"]", @"(", @")", @"{", @"}", @"+", "\"", @"$", @"+", @"|" };

            string searchStr = searchString;

            foreach (string regexChar in regexChars)
            {
                searchStr = searchStr.Replace(regexChar, @"\" + regexChar);    
            }

            searchStr = searchStr.Replace("*", ".*");

            return searchStr;
        }

        public static string CapitalizeFirstLetter(string input)
        {
            return cRegExCap.Replace(input, RegexReplacer);
        }

        private static string RegexReplacer(Match m)
        {
            return m.Captures[0].Value.ToUpper();
        }

        public static string PadIntWithZero(int target, int positions)
        {
            int check = (positions - 1);
            if (System.Math.Pow(10, check) <= target)
                return target.ToString();
            else
            {
                string targetString = target.ToString();
                int newTarget=target;
                for (int i = 2; i <= positions; i++)
                {
                    if ((10 ^ (i - 1)) >= newTarget)
                    {
                        newTarget += (10 ^ (i - 1));
                        targetString = "0" + targetString;
                    }
                    else
                        return targetString;
                }
                return targetString;
            }
        }
    }
}
